package com.cmc.cloud.cmclink.doc.service.impl.so;

import com.cmc.cloud.cmclink.doc.api.document.req.soamendapplyvo.SoAmendApplyCreateReqVO;
import com.cmc.cloud.cmclink.doc.api.document.req.soamendapplyvo.SoAmendApplyPageReqVO;
import com.cmc.cloud.cmclink.doc.api.document.req.soamendapplyvo.SoAmendApplyUpdateReqVO;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.SoAmendApplyErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.SoAmendApplyConvert;
import com.cmc.cloud.cmclink.doc.entity.SoAmendApplyDO;
import com.cmc.cloud.cmclink.doc.mapper.SoAmendApplyMapper;
import com.cmc.cloud.cmclink.doc.service.so.SoAmendApplyService;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;


/**
 * 订舱修改申请审批表主 Service 实现类
 *
 * @author 招商轮船-管理员
 */
@Service
@Validated
public class SoAmendApplyServiceImpl implements SoAmendApplyService {

    @Resource
    private SoAmendApplyMapper soAmendApplyMapper;

    @Override
    public SoAmendApplyDO createSoAmendApply(SoAmendApplyCreateReqVO createReqVO) {
        // 插入
        SoAmendApplyDO soAmendApply = SoAmendApplyConvert.INSTANCE.convert(createReqVO);
        soAmendApplyMapper.insert(soAmendApply);
        return soAmendApply;
    }

    @Override
    public void updateSoAmendApply(SoAmendApplyUpdateReqVO updateReqVO) {
        // 校验存在
        validateSoAmendApplyExists(updateReqVO.getId());
        // 更新
        SoAmendApplyDO updateObj = SoAmendApplyConvert.INSTANCE.convert(updateReqVO);
        soAmendApplyMapper.updateById(updateObj);
    }

    @Override
    public void updateById(SoAmendApplyDO updateObj) {
        soAmendApplyMapper.updateById(updateObj);
    }

    @Override
    public void deleteSoAmendApply(Long id) {
        // 校验存在
        validateSoAmendApplyExists(id);
        // 删除
        soAmendApplyMapper.deleteById(id);
    }

    private void validateSoAmendApplyExists(Long id) {
        if (soAmendApplyMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(SoAmendApplyErrorCodeConstants.SO_AMEND_APPLY_NOT_EXISTS);
        }
    }

    @Override
    public SoAmendApplyDO getSoAmendApply(Long id) {
        return soAmendApplyMapper.selectById(id);
    }

    @Override
    public SoAmendApplyDO getByBookingNo(String bookingNo, List<String> soAmendStatusList) {
        LambdaQueryWrapperX<SoAmendApplyDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(SoAmendApplyDO::getBookingNo, bookingNo);
        queryWrapper.inIfPresent(SoAmendApplyDO::getSoAmendStatus, soAmendStatusList);
        queryWrapper.last("LIMIT 1");
        return soAmendApplyMapper.selectOne(queryWrapper);
    }

    @Override
    public SoAmendApplyDO getByBookingNoNewest(String bookingNo) {
        LambdaQueryWrapperX<SoAmendApplyDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(SoAmendApplyDO::getBookingNo, bookingNo);
        queryWrapper.orderByDesc(SoAmendApplyDO::getApplyTime);
        queryWrapper.last("LIMIT 1");
        return soAmendApplyMapper.selectOne(queryWrapper);
    }

    @Override
    public SoAmendApplyDO getBySoId(Long soId, List<String> soAmendStatusList) {
        LambdaQueryWrapperX<SoAmendApplyDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(SoAmendApplyDO::getSoId, soId);
        queryWrapper.inIfPresent(SoAmendApplyDO::getSoAmendStatus, soAmendStatusList);
        queryWrapper.last("LIMIT 1");
        return soAmendApplyMapper.selectOne(queryWrapper);
    }

    @Override
    public SoAmendApplyDO getBySoId(Long soId) {
        LambdaQueryWrapperX<SoAmendApplyDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(SoAmendApplyDO::getSoId, soId);
        queryWrapper.orderByDesc(SoAmendApplyDO::getCreateTime);
        queryWrapper.last("LIMIT 1");
        return soAmendApplyMapper.selectOne(queryWrapper);
    }

    @Override
    public List<SoAmendApplyDO> getSoAmendApplyList(Collection<Long> ids) {
        return soAmendApplyMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<SoAmendApplyDO> getSoAmendApplyPage(SoAmendApplyPageReqVO reqVO) {
        LambdaQueryWrapperX<SoAmendApplyDO> queryWrapper = new LambdaQueryWrapperX<>();
        return soAmendApplyMapper.selectPage(reqVO, queryWrapper);
    }
}
